//    Sofa Control is a computer remote control designed for handheld devices
//    Copyright (C) 2009 Adrián Romero Corchado
//    http://code.google.com/p/sofacontrol/
//
//    This file is part of Sofa Control.
//
//    Sofa Control is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Sofa Control is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Sofa Control.  If not, see <http://www.gnu.org/licenses/>.

package org.arc.sofacontrol.proc;

import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author adrian
 */
public class KeyProcessor extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    
    // private Robot robot;
    private KeyMap keymap;
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
        try {
            keymap = new KeyMap(new Robot());
        } catch (AWTException ex) {            
            Logger.getLogger(KeyProcessor.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
//            System.out.println("teclazo");
            String key = request.getParameter("value");
            
            Logger.getLogger(KeyProcessor.class.getName()).log(Level.INFO, key);

            
            keymap.exec(key);
            
            //out.println("OK");

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    @Override
    public String getServletInfo() {
        return "Key processor";
    }// </editor-fold>

}
